package cn.kgc.crowdfundingadmin03.controller;

import cn.kgc.crowdfundingadmin03.common.CrowdFundingConstant;
import cn.kgc.crowdfundingadmin03.common.ResultEntity;
import cn.kgc.crowdfundingadmin03.pojo.Advertisement;
import cn.kgc.crowdfundingadmin03.service.AdvertisementService;
import com.github.pagehelper.PageInfo;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author shkstart
 * @create 2020-03-01 13:29
 */
@RestController
public class AdvertisementController {

    @Resource
    AdvertisementService advertisementService;

    //根据分页插询
    @RequestMapping("/select/pageInfo/Advertisement")
    public ResultEntity<PageInfo<Advertisement>>  selectPageInfo(
            @RequestParam(value = "pageNum",required = false,defaultValue = "1") Integer pageNum,
            @RequestParam(value = "pageSize",required = false,defaultValue = "5") Integer pageSize,
            @RequestParam(value = "keyword",required = false,defaultValue = "") String keyword,
            Model model
            ){
        PageInfo<Advertisement> pageInfo = advertisementService.getAllAdvertisement(pageNum, pageSize, keyword);
        model.addAttribute(CrowdFundingConstant.ATTR_NAME_PAGE_INFO,pageInfo);
        return ResultEntity.successWithoutData(pageInfo);
    }
    //查询所有
    @RequestMapping("/select/AdvertisementId")
    public ResultEntity<List<Advertisement>> getCertListByIdList (@RequestBody List<Integer>  advertisementIdList){
        List<Advertisement> all = advertisementService.getAll(advertisementIdList);
        return  ResultEntity.successWithoutData(all);
    }
    //删除
    @RequestMapping("/advertisement/batch/remove")
    public ResultEntity<String> getRoleListByIdList(@RequestBody List<Integer> advertisementIdList) {
        advertisementService.batchRemove(advertisementIdList);
        return ResultEntity.successWithoutData();
    }

    //增加  ajax 传值增加
   /* @RequestMapping("/advertisement/save/role")
    public ResultEntity<String> saveRole(@RequestParam("name") String name,
                                         @RequestParam("iconpath") String iconpath,
                                         @RequestParam("status") Integer status,
                                         @RequestParam("url") String url,
                                         @RequestParam("userid") Integer userid) {
        advertisementService.saveAd(name,iconpath,status,url,userid);
        return ResultEntity.successWithoutData();
    }*/
    //添加

    @RequestMapping("/advertisement/save/role")
    public ResultEntity<String> saveAd(Advertisement ad){
        advertisementService.saveAd(ad);
        return ResultEntity.successWithoutData();
    }
    @RequestMapping("/advertisement/update/role")
    public ResultEntity<String> updateRole(Advertisement advertisement) {
        advertisementService.updateCert(advertisement);

        return ResultEntity.successWithoutData();
    }


}
